﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;
using DslModeling = global::Microsoft.VisualStudio.Modeling;

namespace DSLFactory.VSCTDesigner
{
    partial class VSCTDesignerSerializationHelper
    {
        protected internal override CommandTable CreateModelHelper(Partition modelPartition)
        {
            PropertyAssignment[] assignments = new PropertyAssignment[1];
            assignments[0] = new PropertyAssignment(ElementFactory.IdPropertyAssignment, new Guid("{00000000-0000-0000-0000-000000000000}"));
            return new CommandTable(modelPartition, assignments);
        }

        // Surcharge car on veut forcer tjs le même id pour le bitmaps
        internal protected override VSCTDesignerDiagram CreateDiagramHelper(DslModeling::Partition diagramPartition, DslModeling::ModelElement modelRoot)
        {
            VSCTDesignerDiagram diagram = new VSCTDesignerDiagram(diagramPartition);

            // If model root has not children, create initial elements mapped to swimlanes, and merge them into the root.
            if( DslModeling::DomainRoleInfo.GetAllElementLinks(modelRoot).Count == 0 )
            {
                DslModeling::ElementGroup elementGroup = new DslModeling::ElementGroup(diagramPartition);

                DslModeling::ModelElement mappedElement;
                PropertyAssignment[] assignments = new PropertyAssignment[1];
                assignments[0] = new PropertyAssignment(ElementFactory.IdPropertyAssignment, new Guid("{00000000-0000-0000-0000-000000000001}"));
                mappedElement = new Bitmaps(diagramPartition, assignments);
                elementGroup.Add(mappedElement);
                elementGroup.MarkAsRoot(mappedElement);

                mappedElement = new Commands(diagramPartition);
                elementGroup.Add(mappedElement);
                elementGroup.MarkAsRoot(mappedElement);

                if( diagram.ElementOperations.CanMergeElementGroupPrototype(modelRoot, elementGroup.CreatePrototype()) )
                {
                    diagram.ElementOperations.MergeElementGroup(modelRoot, elementGroup);
                }
                else
                {
                    global::System.Diagnostics.Debug.Fail("Cannot merge swimlane-mapped element(s) into the root model element.  Initial swimlanes will not be created.");
                }
            }


            // If we have unmapped swimlanes then Initialize them also
            diagram.InitializeSwimlanes();

            return diagram;
        }
    }
}
